<%--                                                                       --%>
<%-- Copyright (c) 2005 BroadVision, Inc.  All rights reserved.            --%>
<%--                                                                       --%>
<%-- This software is copyrighted. Under the copyright laws, this software --%>
<%-- may not be copied, in whole or in part, without prior written consent --%>
<%-- of BroadVision, Inc. or its assignees. This software is provided      --%>
<%-- under the terms of a license between BroadVision and the recipient,   --%>
<%-- and its use is subject to the terms of that license.                  --%>
<%--                                                                       --%>
<%-- This software may be protected by one or more U.S. and International  --%>
<%-- patents.  Certain applications of BroadVision One-To-One software are --%>
<%-- covered by U.S. patent 5,710,887.                                     --%>
<%--                                                                       --%>
<%-- TRADEMARKS: BroadVision and BroadVision One-To-One are registered     --%>
<%-- trademarks of BroadVision, Inc., in the United States and the         --%>
<%-- European Community, and are trademarks of BroadVision, Inc., in other --%>
<%-- countries.  The BroadVision logo, is a trademark of BroadVision,      --%>
<%-- Inc., in the United States and other countries. Additionally, IONA    --%>
<%-- and Orbix are trademarks of IONA Technologies, Ltd.  RSA, MD5, and    --%>
<%-- RC2 are trademarks of RSA Data Security, Inc.                         --%>
<%--                                                                       --%>


<%@ page import =  "java.util.HashMap,
                    org.apache.commons.beanutils.PropertyUtils,
                    org.apache.struts.validator.DynaValidatorForm"
%>

<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-html-el.tld" prefix="html-el" %>
<%@ taglib uri="/WEB-INF/struts-bean-el.tld" prefix="bean-el" %>
<%@ taglib uri="/WEB-INF/c.tld" prefix="c" %>

<script language="JavaScript">

  //
  // Get the group popup
  //
  function doGroupPopup(theForm) {
    var DELIM = '%24%23@!';
    var selectedGroupFQNs = "";
    for (var index = 0; index < document.mtUserRoleForm.groupFQNs.length; index++) {
      selectedGroupFQNs += document.mtUserRoleForm.groupFQNs.options[index].value + DELIM;
    }

    var DELIM = '%24%23@!';
    var popupUrl = '<c:url value="/mt/groupMultiPopup.do"/>';

    popupUrl += '?callback=setNewGroups';
    popupUrl += '&parentFormName=' + 'abc';
    popupUrl += '&minSelection=0&multiSelect=true&recursiveCheck=true';
    popupUrl += '&existingGroupFQNs=' + selectedGroupFQNs;
    popupUrl += '&orgAdminCheck=true';
    
    var windowFeatures = 'width=600,height=600,scrollbars=yes,resizable=yes';
    var popupWindow = window.open(popupUrl, 'GroupPopup', windowFeatures);
    if (popupWindow != null) {
      if (popupWindow.opener == null) {
        popupWindow.opener = self;
      }
    }
    popupWindow.focus();
  }

  //
  // Call back function
  //
  function setNewGroups(ids, names) {
    document.mtUserRoleForm.groupFQNs.length = 0;
    for (var index = 0; index < ids.length; index++) {
      var option = new Option(" ");
      option.text = names[index];
      option.value = ids[index];
      document.mtUserRoleForm.groupFQNs.options[index] = option;
    }
    document.mtUserRoleForm.changeGroupFQNs.value = true;
  }

  //
  // Get the channel popup
  //
  function doChannelPopup(theForm) {
    var selectedIds = "";
    for (var index = 0; index < document.mtUserRoleForm.channelIDs.length; index++) {
      selectedIds += document.mtUserRoleForm.channelIDs.options[index].value + ";";
    }
    doPickerPopupAction('<c:url value="/mt/categoryPickerPopupMultiSelect.do"/>', 'setNewChannels', 301, null, selectedIds, null, null, null, null, null, null, 0);
  }

  //
  // Call back function
  //
  function setNewChannels(ids, names) {
    document.mtUserRoleForm.channelIDs.length = 0;
    for (var index = 0; index < ids.length; index++) {
      var option = new Option(" ");
      option.text = names[index];
      option.value = ids[index];
      document.mtUserRoleForm.channelIDs.options[index] = option;
    }
    document.mtUserRoleForm.changeChannelIDs.value = true;
  }

  function doReset(theForm) {
    if (theForm != null) {
      theForm.channelIDs.length = 0;
      theForm.groupFQNs.length = 0;
<c:forEach var="oneRole" items="${mtUserRoleView.map.roleList}" >
  <c:if test="${oneRole.map.name == 'ChannelAdmin'}">
    <c:forEach var="oneVal" items="${oneRole.map.associatedObjects}" varStatus="status">
      var option = new Option(" ");
      option.text = '<c:out value="${oneVal.map.name}"/>';
      option.value = '<c:out value="${oneVal.map.id}"/>';
      theForm.channelIDs.options[<c:out value="${status.index}"/>] = option;
    </c:forEach>
  </c:if>
  <c:if test="${oneRole.map.name == 'OrganizationAdmin'}">
    <c:forEach var="oneVal" items="${oneRole.map.associatedObjects}" varStatus="status">
      var option = new Option(" ");
      option.text = '<c:out value="${oneVal.map.name}"/>';
      option.value = '<c:out value="${oneVal.map.fqn}"/>';
      theForm.groupFQNs.options[<c:out value="${status.index}"/>] = option;
    </c:forEach>
  </c:if>
</c:forEach>
    }
    return true;
  }

  function doCancel(theForm) {
    //var ok = confirm('<bean-el:message key="confirmCancel"  bundle="mt.messages"/>');
    if (theForm != null) {
      theForm.action='<html-el:rewrite page="/mt/userRoleView.do" />';
      theForm.submit();
      return true;
    }
    return false;
  }

  function doUserRoleEditAction(theForm) {
    theForm.action = '<html-el:rewrite page="/mt/userRoleEdit.do"/>';
    theForm.submit();
    return true;
  }

  function doSubmit(form) {
    var size = form.channelIDs.length;
    for (var index = 0; index < size ; index++) {
      form.channelIDs.options[index].selected = true;
    }

    size = form.groupFQNs.length;
    for (var index = 0; index < size ; index++) {
      form.groupFQNs.options[index].selected = true;
    }
    form.submit();    
  }


</script>

<%@include file="/mt/common/pickerPopupHelper.jsp" %>

<%
  java.util.HashMap map = new java.util.HashMap();
  DynaValidatorForm mtUserRoleForm = (DynaValidatorForm) request.getAttribute("mtUserRoleForm");
  map.put("userId", (Long) PropertyUtils.getSimpleProperty(mtUserRoleForm, "userId"));
  pageContext.setAttribute("pagetabParam", map, PageContext.PAGE_SCOPE);
%>

<html-el:form action="/mt/userRoleSave.do">
<html-el:hidden name="mtUserRoleForm" property="userId"/>
<html-el:hidden name="mtUserRoleForm" property="changeChannelIDs"/>
<html-el:hidden name="mtUserRoleForm" property="changeGroupFQNs"/>
<html-el:hidden name ="mtUserRoleForm" property="cancelAction" value="/mt/userSearch.do" />
<table class="zoneFive" width="100%" border="0" cellspacing="0" cellpadding="0" summary="content area">
  <tr>
    <td valign="top" width="100%">
      <div class="pageTitle">
        <bean-el:message key="mt.user.edit.page.title" bundle="mt.messages"/>:
        <c:out value="${mtUserRoleView.map.formattedName}"/>
      </div> 
      <div class="errorMessage"><html-el:errors bundle="mt.messages" /></div>
      <%@include file="/mc/common/pagetab.jsp" %>
      <div class="pageSubtitle">
        <span class="left">
          <bean-el:message key="mt.user.role.roles" bundle="mt.messages"/>
        </span>
        <span class="right">
          &nbsp;
        </span>
      </div>
      <table class="dataTable" border="0" cellspacing="1" cellpadding="2" summary="Category" width="100%">
        <tr class="listHeader"> 
          <th id="SubCategoryName" align="left" width="25%">
            <bean-el:message key="mt.user.role" bundle="mt.messages"/>
          </th>
          <th id="SubCategoryName" align="left" width="50%">
            <bean-el:message key="mt.user.role.description" bundle="mt.messages"/>
          </th>
          <th id="Checkboxes" align="center" width="5%">
            <bean-el:message key="mt.user.role.selected" bundle="mt.messages"/>
          </th>
          <th id="AssocObjects" align="left"  width="15%">
            <bean-el:message key="mt.user.role.associatedObjects" bundle="mt.messages"/>
          </th>
        </tr>
<c:if test="${mtUserRoleView.map.isOrgAdmin}">
  <c:forEach var="oneR" items="${mtUserRoleForm.map.assignedRoles}">
    <c:if test="${oneR != 'OrganizationAdmin' && oneR != 'ChannelAdmin' && oneR != 'MicrositeCreator' && oneR != 'ContentPublisher'}">
        <input type="hidden" name="assignedRoles" value='<c:out value="${oneR}"/>'>
    </c:if>
  </c:forEach>
</c:if>
<c:forEach var="oneRole" items="${mtUserRoleView.map.roleList}" varStatus="status">
  <c:choose>
   <c:when test="${(status.count + 1) % 2 == 0}">
        <tr class="listEven">
   </c:when> 
   <c:otherwise>
        <tr class="listOdd">
   </c:otherwise>
  </c:choose>
          <td headers="SubCategoryName">
            <c:out value="${oneRole.map.friendlyName}" />
          </td>
          <td headers="SubCategoryName">
            <c:out value="${oneRole.map.description}" />
          </td>
          <td headers="Checkboxes" align="center">
  <c:choose>
    <c:when test="${oneRole.map.name == 'UserAdmin'}">
      <c:choose>
        <c:when test="${mtUserRoleView.map.userAlias == 'bvadmin'}">
            <input type="checkbox" name="assignedRoles" value="UserAdmin" checked="checked" disabled="true"/>
            <input type="hidden" name="assignedRoles" value="UserAdmin"/>
        </c:when>
        <c:otherwise>
          <c:choose>
            <c:when test="${!mtUserRoleView.map.isOrgAdmin}">
            <html:multibox name="mtUserRoleForm" property="assignedRoles">
              <c:out value="${oneRole.map.name}"/>
            </html:multibox>
            </c:when>
            <c:otherwise>
            <html:multibox name="mtUserRoleForm" property="assignedRoles" disabled="true">
              <c:out value="${oneRole.map.name}"/>
            </html:multibox>
            </c:otherwise>
          </c:choose>
        </c:otherwise>
      </c:choose>
    </c:when>
    <c:when test="${oneRole.map.name == 'ContentAdmin'}">
      <c:if test="${mtUserRoleView.map.isContentServices}">
        <c:choose>
          <c:when test="${mtUserRoleView.map.isOrgAdmin}">
            <html:multibox name="mtUserRoleForm" property="assignedRoles" disabled="true">
              <c:out value="${oneRole.map.name}"/>
            </html:multibox>
          </c:when>
          <c:otherwise>
            <html:multibox name="mtUserRoleForm" property="assignedRoles">
              <c:out value="${oneRole.map.name}"/>
            </html:multibox>
          </c:otherwise>
        </c:choose>
      </c:if>
    </c:when>
    <c:when test="${oneRole.map.name == 'PersonalizationAdmin'}">
      <c:if test="${mtUserRoleView.map.isEmarketingServices}">
        <c:choose>
          <c:when test="${mtUserRoleView.map.isOrgAdmin}">
            <html:multibox name="mtUserRoleForm" property="assignedRoles" disabled="true">
              <c:out value="${oneRole.map.name}"/>
            </html:multibox>
          </c:when>
          <c:otherwise>
            <html:multibox name="mtUserRoleForm" property="assignedRoles">
              <c:out value="${oneRole.map.name}"/>
            </html:multibox>
          </c:otherwise>
        </c:choose>
      </c:if>
    </c:when>
    <c:when test="${oneRole.map.name == 'OrganizationAdmin' || oneRole.map.name == 'ChannelAdmin'}">
      <c:if test="${mtUserRoleView.map.isPortal}">
        <c:if test="${oneRole.map.name == 'OrganizationAdmin'}">
            <input class="pageButton" type="button" onclick="JavaScript:doGroupPopup(this.form)" value='<bean-el:message key="button.selectGroups" bundle="mt.messages"/>'/>
        </c:if>
        <c:if test="${oneRole.map.name == 'ChannelAdmin'}">
            <input class="pageButton" type="button" onclick="JavaScript:doChannelPopup('mtUserRoleForm')" value='<bean-el:message key="button.selectChannels" bundle="mt.messages"/>'/>
        </c:if>
      </c:if>
    </c:when>
    <c:when test="${oneRole.map.name == 'MicrositeCreator'}">
      <c:if test="${mtUserRoleView.map.isPortal}">
            <html:multibox name="mtUserRoleForm" property="assignedRoles">
              <c:out value="${oneRole.map.name}"/>
            </html:multibox>
      </c:if>
    </c:when>
    <c:when test="${oneRole.map.name == 'ContentPublisher'}">
            <html:multibox name="mtUserRoleForm" property="assignedRoles">
              <c:out value="${oneRole.map.name}"/>
            </html:multibox>
    </c:when>
    <c:when test="${oneRole.map.name == 'ServiceAdministrator' || oneRole.map.name == 'NavigationAdmin'}">
      <c:if test="${mtUserRoleView.map.isPortal}">
        <c:choose>
          <c:when test="${mtUserRoleView.map.isOrgAdmin}">
            <html:multibox name="mtUserRoleForm" property="assignedRoles" disabled="true">
              <c:out value="${oneRole.map.name}"/>
            </html:multibox>
          </c:when>
          <c:otherwise>
            <html:multibox name="mtUserRoleForm" property="assignedRoles">
              <c:out value="${oneRole.map.name}"/>
            </html:multibox>
          </c:otherwise>
        </c:choose>
      </c:if>
    </c:when>
  
    <c:otherwise>
      <c:choose>
        <c:when test="${mtUserRoleView.map.isOrgAdmin}">
            <html:multibox name="mtUserRoleForm" property="assignedRoles" disabled="true">
              <c:out value="${oneRole.map.name}"/>
            </html:multibox>
        </c:when>
        <c:otherwise>
            <html:multibox name="mtUserRoleForm" property="assignedRoles">
              <c:out value="${oneRole.map.name}"/>
            </html:multibox>
        </c:otherwise>
      </c:choose>
    </c:otherwise>
  </c:choose>
          </td>
          <td align="left" headers="AssocObjects">
  <c:if test="${oneRole.map.name == 'OrganizationAdmin'}">
            <html-el:select multiple="true" size="3" property="groupFQNs">
              <html-el:optionsCollection name="oneRole" property="associatedObjects" value="fqn" label="name"/>
            </html-el:select>
  </c:if>
  <c:if test="${oneRole.map.name == 'ChannelAdmin'}">
            <html-el:select multiple="true" size="3" property="channelIDs">
              <html-el:optionsCollection name="oneRole" property="associatedObjects" value="id" label="name"/>
            </html-el:select>
  </c:if>
          </td>
        </tr>
</c:forEach>
      </table>
      <div class="pageButtonWrap" align="right">
        <html-el:reset styleClass="inputButton" property="resetBtn" onclick="return doReset(this.form)">
          <bean-el:message key="button.reset" bundle="mt.messages" />
        </html-el:reset>
        &nbsp;&nbsp;
        <html-el:button styleClass="inputButton" property="cancelBtn" onclick="return doCancel(this.form)">
          <bean-el:message key="button.cancel" bundle="mt.messages"/>
        </html-el:button>
        &nbsp;
        <html-el:submit styleClass="inputButton" property="submitBtn" onclick="return doSubmit(this.form)">
          <bean-el:message key="button.save" bundle="mt.messages"/>
        </html-el:submit>
      </div> 
    </td>
  </tr>
</table>
</html-el:form>
